package com.bjsxt;
/**线程join()的使用*/

class C implements Runnable{

    @Override
    public void run() {
        for(int i=0;i<10;i++){
            System.out.println(Thread.currentThread().getName()+" "+i);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
class D implements Runnable{
    @Override
    public void run() {
        for (int i=0;i<20;i++){
            System.out.println(Thread.currentThread().getName()+" "+i);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
public class JoinThread02 {
    public static void main(String[] args) {
        Thread t = new Thread(new C());
        Thread t1 = new Thread(new D());
        t.start();
        t1.start();
        //主线程
        for (int i=0;i<10;i++){
            System.out.println(Thread.currentThread().getName()+' '+i);
            //主线程与c联合
            if(i == 2){
                try {
                    t.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }


}
